package joey.project1.model

import java.util.Properties

class Model1 {
	private static final Model1 instance = new Model1()
	private Properties prop = new Properties()

	//properties key
	private String _lotteryPath = 'lotteryPath'
	private String _centerImage = 'centerImage'
	private String _imgWidth = 'imgWidth'
	private String _imgHeight = 'imgHeight'
	private String _allowFile = 'allowFile'
	private String _wav_drum_start = 'wav_drum_start'
	private String _wav_drum_winner = 'wav_drum_winner'
	private String _wav_roll = 'wav_roll'
	private String _wav_start = 'wav_start'
	private String _wav_winner = 'wav_winner'
	private String _time_lottery_start = 'time_lottery_start'
	private String _time_lottery_end = 'time_lottery_end'
	private String _time_lottery_roll = 'time_lottery_roll'
	private String _time_lottery_winner = 'time_lottery_winner'
	private String _time_select = 'time_select'
	private String _lottery_roll_loop = 'lottery_roll_loop'
	private String _isShowFileName = 'isShowFileName'
	private String _imgBorderWidth = 'imgBorderWidth'
	private String _backgroundImage = 'backgroundImage'
	private String _backgroundImage_width = 'backgroundImage_width'
	private String _backgroundImage_height = 'backgroundImage_height'
	private String _backgroundImage1 = 'backgroundImage1'
	private String _backgroundImage_width1 = 'backgroundImage_width1'
	private String _backgroundImage_height1 = 'backgroundImage_height1'
	private String _backgroundImage2 = 'backgroundImage2'
	private String _backgroundImage_width2 = 'backgroundImage_width2'
	private String _backgroundImage_height2 = 'backgroundImage_height2'
	private String _backgroundImage3 = 'backgroundImage3'
	private String _backgroundImage_width3 = 'backgroundImage_width3'
	private String _backgroundImage_height3 = 'backgroundImage_height3'
	private String _backgroundImage4 = 'backgroundImage4'
	private String _backgroundImage_width4 = 'backgroundImage_width4'
	private String _backgroundImage_height4 = 'backgroundImage_height4'

	//抽獎照片
	private List<File> lotteryFiles = []
	private List<File> winnerFiles = []
	private Random random = new Random()
	private File wav_drum_start
	private File wav_drum_winner
	private File wav_roll
	private File wav_start
	private File wav_winner
	private WavPlay play_wav_drum_start
	private WavPlay play_wav_drum_winner
	private WavPlay play_wav_roll
	private WavPlay play_wav_start
	private WavPlay play_wav_winner
	private int time_lottery_start
	private int time_lottery_end
	private int time_lottery_roll
	private int time_lottery_winner
	private int time_select
	private int lottery_roll_loop

	private Model1(){
		readProp()

		String allowFile = prop.get(_allowFile)
		File lotteryPath = new File(prop.get(_lotteryPath))
		LinkedList<File> dir = new LinkedList<File>()

		wav_drum_start = new File(prop.get(_wav_drum_start))
		wav_drum_winner = new File(prop.get(_wav_drum_winner))
		wav_roll = new File(prop.get(_wav_roll))
		wav_start = new File(prop.get(_wav_start))
		wav_winner = new File(prop.get(_wav_winner))

		play_wav_drum_start = new WavPlay(file:wav_drum_start)
		play_wav_drum_winner = new WavPlay(file:wav_drum_winner)
		play_wav_roll = new WavPlay(file:wav_roll)
		play_wav_start = new WavPlay(file:wav_start)
		play_wav_winner = new WavPlay(file:wav_winner)

		lottery_roll_loop = prop.get(_lottery_roll_loop) as Integer
		time_lottery_start = prop.get(_time_lottery_start) as Integer
		time_lottery_end = prop.get(_time_lottery_end) as Integer
		time_lottery_roll = prop.get(_time_lottery_roll) as Integer
		time_lottery_winner = prop.get(_time_lottery_winner) as Integer
		time_select = prop.get(_time_select) as Integer

		dir << lotteryPath
		while(dir){
			File firstDir = dir.removeFirst()
			firstDir.listFiles().each { File listFile ->
				if(listFile.isDirectory()){
					dir << listFile
				}else{
					String name = listFile.getName()
					if(name.indexOf('.') > 0){
						String ext = name.substring(name.indexOf('.') + 1, name.length())
						if(ext.matches(allowFile))
							lotteryFiles << listFile
					}
				}
			}
		}
	}

	public static Model1 getInstance(){
		return instance
	}

	private Properties readProp(){
		InputStream input = new FileInputStream('./setup/joey.properties')
		InputStreamReader reader = new InputStreamReader(input,'UTF-8')

		prop.load(reader)

		reader.close()
		input.close()

		return prop
	}

	private void saveProp(){
		FileOutputStream oStream = new FileOutputStream('./setup/joey.properties')
		OutputStreamWriter writer = new OutputStreamWriter(oStream,'UTF-8')

		prop.store(writer,'')

		writer.close()
		oStream.close()
	}

	public String getCenterImage(){
		return prop.get(_centerImage)
	}

	public int getImgWidth(){
		return prop.get(_imgWidth) as Integer
	}

	public int getImgHeight(){
		return prop.get(_imgHeight) as Integer
	}

	public int randomIndex(int max){
		return random.nextInt(max)
	}

	public boolean isShowFileName(){
		return new Boolean(prop.get(_isShowFileName))
	}

	public int getImgBorderWidth(){
		return prop.get(_imgBorderWidth) as Integer
	}

	public List<File> randomLotteryFiles(){
		List<File> files = []
		while(files.size() < 8){
			if(lotteryFiles.size() < 8)
				throw new RuntimeException('剩下的人數不足8個人，不能參加抽獎')
			int index = random.nextInt(lotteryFiles.size())
			File imgFile = lotteryFiles.get(index)
			if(files.contains(imgFile) || winnerFiles.contains(imgFile))
				continue
			files << imgFile
		}
		return files
	}

	public void winner(File file){
		lotteryFiles.remove(file)
		winnerFiles << file
	}

	public String filterName(File file){
		String name = file.name
		name = name.substring(0,name.indexOf('.'))
		if(name.matches(/\w+/))
			return ' '
		else
			return name
	}

	public String fileName(File file){
		String name = file.name
		name = name.substring(0,name.indexOf('.'))
		return name
	}

	public Map backgroundImage(){
		Map attr = [:]
		String bgImg = prop.get(_backgroundImage)
		if(bgImg){
			attr.image = new File(bgImg)
			attr.width = prop.get(_backgroundImage_width) as Integer
			attr.height = prop.get(_backgroundImage_height) as Integer
		}
		return attr
	}

	public Map backgroundImage1(){
		Map attr = [:]
		String bgImg = prop.get(_backgroundImage1)
		if(bgImg){
			attr.image = new File(bgImg)
			attr.width = prop.get(_backgroundImage_width1) as Integer
			attr.height = prop.get(_backgroundImage_height1) as Integer
		}
		return attr
	}

	public Map backgroundImage2(){
		Map attr = [:]
		String bgImg = prop.get(_backgroundImage2)
		if(bgImg){
			attr.image = new File(bgImg)
			attr.width = prop.get(_backgroundImage_width2) as Integer
			attr.height = prop.get(_backgroundImage_height2) as Integer
		}
		return attr
	}

	public Map backgroundImage3(){
		Map attr = [:]
		String bgImg = prop.get(_backgroundImage3)
		if(bgImg){
			attr.image = new File(bgImg)
			attr.width = prop.get(_backgroundImage_width3) as Integer
			attr.height = prop.get(_backgroundImage_height3) as Integer
		}
		return attr
	}

	public Map backgroundImage4(){
		Map attr = [:]
		String bgImg = prop.get(_backgroundImage4)
		if(bgImg){
			attr.image = new File(bgImg)
			attr.width = prop.get(_backgroundImage_width4) as Integer
			attr.height = prop.get(_backgroundImage_height4) as Integer
		}
		return attr
	}
}
